﻿using System;
using System.Collections.Generic;
using RssReader.Common;
using RssReader.DataModel;
using RssReader.Feeds;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The Group Detail Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234229

namespace RssReader
{
    /// <summary>
    /// A page that displays an overview of a single group, including a preview of the items
    /// within the group.
    /// </summary>
    public sealed partial class GroupDetailPage : LayoutAwarePage
    {
        private static Type _previousFrame;

        static public Dictionary<string, object> Session_state
        {
            get { return SessionState; }
        }
        
        static private readonly Dictionary<string, object> SessionState = new Dictionary<string, object>();
        public GroupDetailPage()
        {
            this.InitializeComponent();
            NavigationCacheMode = NavigationCacheMode.Enabled;
        }
        /// <summary>
        /// Populates the page with content passed during navigation.  Any saved state is also
        /// provided when recreating a page from a prior session.
        /// </summary>
        /// <param name="navigationParameter">The parameter value passed to
        /// <see cref="Frame.Navigate(Type, Object)"/> when this page was initially requested.
        /// </param>
        /// <param name="pageState">A dictionary of state preserved by this page during an earlier
        /// session.  This will be null the first time a page is visited.</param>
        protected override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
        {
            MainGrid.Background = new SolidColorBrush(App.ARGBColor);
            const string pageStateKey = "groupdetail";

            
            var frameState = SuspensionManager.SessionState;

            if (navigationParameter != null)
            {
                frameState[pageStateKey] = navigationParameter;
            }
            if (frameState != null && frameState.ContainsKey(pageStateKey)) navigationParameter = frameState[pageStateKey];

            var tuple = (Tuple<Category, Type>)navigationParameter;

            if (tuple != null)
            {
                var selectedCategory = tuple.Item1;
                _previousFrame = tuple.Item2;
                if (selectedCategory != null)
                {
                    DefaultViewModel["Group"] = selectedCategory;
                    DefaultViewModel["Items"] = selectedCategory.Feed.Items;
                    pageTitle.Text = selectedCategory.Name;
                }
            }
            bounds.Text = Fix_item_height_for_current_screen_resolution();
        }

        /// <summary>
        /// Invoked when the back button is pressed.
        /// </summary>
        /// <param name="sender">The Button instance representing the back button.</param>
        /// <param name="e">Event data describing how the button was clicked.</param>
        protected override void GoBack(object sender, RoutedEventArgs e)
        {
            // Return the application to the state it was in before search results were requested

            if (_previousFrame == null) _previousFrame = typeof(ItemsPage);

            var rootFrame = new Frame();
            rootFrame.Navigate(_previousFrame);

            // Place the frame in the current Window and ensure that it is active
            Window.Current.Content = rootFrame;
        }

        /// <summary>
        /// Invoked when an item is clicked.
        /// </summary>
        /// <param name="sender">The GridView (or ListView when the application is snapped)
        /// displaying the item clicked.</param>
        /// <param name="e">Event data that describes the item clicked.</param>
        void Item_view_item_click(object sender, ItemClickEventArgs e)
        {
            var itemDetailsViewHelper = new ItemDetailsViewHelper().Get_selected_item_and_show_item_details_view(e, App.ItemsKeyString,Window.Current.Content);
            this.Frame.Navigate(typeof(ItemDetailPage), itemDetailsViewHelper);
        }
    }
}
